package team.zero.lemon.module.sys.web;

import team.zero.lemon.common.security.session.model.Session;
import team.zero.lemon.module.sys.entity.*;
import team.zero.lemon.common.authz.annotation.AuthPermission;
import team.zero.lemon.common.persistence.Page;
import team.zero.lemon.common.exception.CustomException;
import team.zero.lemon.common.security.session.model.Function;
import team.zero.lemon.common.web.BaseController;
import team.zero.lemon.common.authz.annotation.AuthPassport;
import team.zero.lemon.module.sys.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * Created by bolin on 2015/5/18.
 */
@Controller
@ResponseBody
@AuthPassport
@RequestMapping(value = "${adminPath}/sys/user", method = {RequestMethod.POST, RequestMethod.GET})
public class UserController extends BaseController {

    @Autowired
    private UserService userService;

    @RequestMapping("/login")
    public Session authlogin(@RequestBody User user) throws CustomException {
        return userService.login(user);
    }

    @RequestMapping("/logout")
    public boolean logout(@RequestBody User user) throws CustomException {
        return userService.logout(user.getToken());
    }

    @RequestMapping("/passwd")
    public String passwd(@RequestBody User user) throws CustomException {
        return userService.passwd(user);
    }

    @AuthPassport
    @RequestMapping("/menu")
    public List<Function> findFunctionByToken(@RequestBody User user) {
        return userService.findFunctionByToken(user.getToken());
    }

    @AuthPassport
    @RequestMapping("/query/page")
    @AuthPermission("sys:user:view")
    public Page<User> findUser(@RequestBody User user) {
        return userService.findPage(user);
    }

    @AuthPassport
    @RequestMapping("/save")
    @AuthPermission("sys:user:edit")
    public int SaveUser(@RequestBody User user) {
        return userService.SaveUser(user);
    }

    @AuthPassport
    @RequestMapping("/delete/ids")
    @AuthPermission("sys:user:delete")
    public int deleteUser(@RequestBody User user) {
        return userService.deleteLogicMulti(user);
    }

    @AuthPassport
    @RequestMapping("/query/all")
    @AuthPermission("sys:user:view")
    public List<User> findAllUsers() {
        return userService.findAll();
    }

    @AuthPassport
    @RequestMapping("/role/query/roleId")
    @AuthPermission("sys:user:role:view")
    public List<UserRole> findUserByRoleId(@RequestBody UserRole userRole) {
        return userService.findUserByRoleId(userRole);
    }

    @AuthPassport
    @RequestMapping("/role/query/userId")
    public List<UserRole> findUserByUserId(@RequestBody UserRole userRole) {
        return userService.findUserByUserId(userRole);
    }

    @AuthPassport
    @RequestMapping("/role/save")
    @AuthPermission("sys:user:role:edit")
    public int saveUserRole(@RequestBody List<UserRole> userRoles) {
        return userService.saveUserRole(userRoles);
    }

    @AuthPassport
    @RequestMapping("/role/delete/roleId")
    @AuthPermission("sys:user:role:delete")
    public int saveUserRoleDelete(@RequestBody List<UserRole> userRoles) {
        return userService.userRoleDelete(userRoles);
    }

    @AuthPassport
    @RequestMapping("/role/delete/userId")
    @AuthPermission("sys:user:role:delete")
    public int deleteUserRoleByUserId(@RequestBody UserRole userRole) {
        return userService.deleteUserRoleByUserId(userRole);
    }

    @AuthPassport
    @RequestMapping("/job/org/query/userId/jobId")
    public List<UserJobOrg> findUserJobOrgByUserIdJobId(@RequestBody UserJobOrg userRoleOrg) {
        return userService.findUserJobOrgByUserIdJobId(userRoleOrg);
    }

    @AuthPassport
    @RequestMapping("/job/org/save")
    public int saveUserJobOrg(@RequestBody List<UserJobOrg> userRoleOrgs) {
        return userService.saveUserJobOrg(userRoleOrgs);
    }

    @AuthPassport
    @RequestMapping("/job/org/delete/userId/jobId")
    public int deleteUserJobOrg(@RequestBody List<UserJobOrg> userJobOrg) {
        return userService.deleteUserJobOrg(userJobOrg);
    }
}

